﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TrabalhoWeb
{
    public partial class UserControlCadContas : System.Web.UI.UserControl
    {
        DAL.Contas contas = new DAL.Contas();
        DAL.Categoria cat = new DAL.Categoria();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PreencheGrid();
            }
        }

        private void PreencheGrid()
        {
            try
            {
                grvContas.DataSource = contas.Conta();
                grvContas.DataBind();
                ddlCategoria.DataSource = cat.Categorias();
                ddlCategoria.DataBind();
            }catch(Exception ex)
            {
                ExibirAlerta("Erro ao carregar:\n"+ex.Message);
            }
        }

        public void ExibirAlerta(string strTexto)
        {
            System.Web.UI.ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "MSG", "alert('" + strTexto + "')", true);
        }

        protected void grvContas_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch(e.CommandName)
            {
                case "excluir":
                    try
                    {
                        contas.DeletaConta(Convert.ToInt32(e.CommandArgument));
                        PreencheGrid();
                    }
                    catch (Exception ex)
                    {
                        ExibirAlerta("Erro ao deletar:\n" + ex.Message);
                    }
                    break;
                case "editar":
                    PreencheGrid();
                    DataTable dt = new DataTable();
                    dt = contas.ListarContas(Convert.ToInt32(e.CommandArgument));
                    lblId.Text = dt.Rows[0]["ID"].ToString();
                    txtNome.Text = dt.Rows[0]["NOME"].ToString();
                    txaDescricao.InnerText = dt.Rows[0]["DESCRICAO"].ToString();
                    ddlCategoria.SelectedValue = dt.Rows[0]["ID_CATEGORIA_CONTA"].ToString();
                    cldDtVencimento.SelectedDate = Convert.ToDateTime(dt.Rows[0]["DATA_VENCIMENTO"].ToString());
                    txtValor.Text = dt.Rows[0]["VALOR"].ToString();
                    string f = dt.Rows[0]["FOTO"].ToString();
                    if(f!="")
                    {
                        imgFoto.ImageUrl="Image\\"+f;
                        imgFoto.Attributes.Add("FOTO",f);
                    }

                    chkPago.Checked = dt.Rows[0]["STATUS"]=="true"?true:false;
                    break;
                    
            }
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            try
            {
                string foto = "";
                if(fluFoto.HasFile)
                {
                    foto = fluFoto.FileName;
                    fluFoto.SaveAs(MapPath("Images\\")+fluFoto.FileName);                   
                }
                contas.InsereAlteraContas(lblId.Text=="0"?null:(int?)int.Parse(lblId.Text),
                    txtNome.Text,
                    txaDescricao.InnerText,
                    int.Parse(ddlCategoria.SelectedValue),
                    cldDtVencimento.SelectedDate,
                    Convert.ToDouble(txtValor.Text),
                    foto,
                    chkPago.Checked
                    );
                PreencheGrid();
            }catch(Exception ex)
            {
                ExibirAlerta("Erro ao salvar:\n"+ex.Message);
            }
        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {

        }

        protected void grvContas_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
        }
    }
}